import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ButtonGroup;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;


public class checkoutDialog extends JDialog{
	
	// GUI
	private backgroundPanel panel;
	private JLabel roomLabel;
	private JComboBox roomComboBox;
	private JLabel nameLabel;
	private JTextField nameTextField;
	private JLabel totalTimeLabel;
	private JTextField timeTextField ;
	private JLabel serviceLabel;
	private JTextField serviceTextField;
	private JLabel payLabel;
	private JTextField moneyTextField ;
	private JButton checkoutButton;
	private JButton cancelButton;
	
	// handlers
	private RoomComboBoxHandler roomComboBoxHandler;
	private CheckoutHandler checkoutHandler;
	private CancelHandler cancelHandler;
	
	// data control
	private roomManager rm;
	private host h;
	private UserManager um ;
	
	public checkoutDialog(roomManager initRm){
		rm = initRm;
		layoutGUI();
	}

	private void layoutGUI() {
		// TODO Auto-generated method stub
		this.setTitle("Checkout");
		this.setSize(400, 300);
		this.setResizable(false);
		panel = new backgroundPanel("background2.jpg",0,0,400,300);
		roomLabel = new JLabel("Room: ");
		roomLabel.setForeground(Color.WHITE);
		roomComboBox = new JComboBox(rm.findRoomState(1));
		nameLabel = new JLabel("Name: ");
		nameLabel.setForeground(Color.WHITE);
		nameTextField = new JTextField("");
		nameTextField.setEnabled(false);
		totalTimeLabel = new JLabel("Total Minutes: ");
		totalTimeLabel.setForeground(Color.WHITE);
		timeTextField = new JTextField ("");
		timeTextField.setEnabled(false);
		serviceLabel = new JLabel("Service Fee: ");
		serviceLabel.setForeground(Color.WHITE);
		serviceTextField = new JTextField("");
		serviceTextField.setEnabled(false);
		payLabel = new JLabel("Need to pay: ");
		payLabel.setForeground(Color.WHITE);
		moneyTextField = new JTextField ("");
		moneyTextField.setEnabled(false);
		checkoutButton = new JButton("Checkout");
		cancelButton = new JButton("Cancel");
		
		// add handlers
		roomComboBoxHandler = new RoomComboBoxHandler();
		checkoutHandler = new CheckoutHandler();
		cancelHandler = new CancelHandler();
		roomComboBox.addActionListener(roomComboBoxHandler);
		checkoutButton.addActionListener(checkoutHandler);
		cancelButton.addActionListener(cancelHandler);
		
		
		GridBagLayout layout = new GridBagLayout();
		panel.setLayout(layout);
		addComponent(roomLabel,			panel,	0,	0,	1,	1);
		addComponent(roomComboBox,		panel,	1,	0,	3,	1);
		addComponent(nameLabel,			panel,	0,	1,	1,	1);
		addComponent(nameTextField,		panel,	1,	1,	3,	1);
		addComponent(totalTimeLabel,	panel,	0,	2,	1,	1);
		addComponent(timeTextField ,	panel,	1,	2,	3,	1);
		addComponent(serviceLabel,		panel,	0,	3,	1,	1);
		addComponent(serviceTextField,	panel,	1,	3,	3,	1);
		addComponent(payLabel,			panel,	0,	4,	1,	1);
		addComponent(moneyTextField ,	panel,	1,	4,	3,	1);	
		addComponent(checkoutButton,	panel,	1,	5,	1,	1);
		addComponent(cancelButton,		panel,	2,	5,	1,	1);
		
		this.add(panel);
		// size and locate the dialog at the center of the screen
		this.pack();
		this.setModal(true);
		int w = getWidth();
		int h = getHeight();
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenWidth = screenSize.width;
		int screenHeight = screenSize.height;
		int x = (screenWidth - w)/2;
		int y = (screenHeight - h)/2;
		setLocation(x,y);
	}
	
	// method helping GridBagLayout
	public void addComponent(	JComponent jc, Container c, 
			int x, int y, int w, int h){
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = x;
		gbc.gridy = y;
		gbc.gridwidth = w;
		gbc.gridheight = h;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(5, 5, 5, 5);
		c.add(jc, gbc);
	}
	
	/** for test
	public static void main (String[] args){
		checkoutDialog temp = new checkoutDialog(new roomManager());
		temp.setVisible(true);
	}
	*/
	
	class RoomComboBoxHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			if (( (JComboBox) arg0.getSource()).getSelectedItem().equals(-1)){
				nameTextField.setText("");
				timeTextField.setText("");
				serviceTextField.setEnabled(false);
				moneyTextField.setEnabled(false);
			}
			else {
				int roomNum = (Integer) ((JComboBox) arg0.getSource()).getSelectedItem();
				h = rm.searchRoom(roomNum).getCurrent();
				nameTextField.setText(h.getName());
				timeTextField.setText(h.getTotalMin()+"");
				serviceTextField.setEnabled(true);
				moneyTextField.setEnabled(true);
			}
		}
		
	}
	
	
	class CheckoutHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			int ans = JOptionPane.showConfirmDialog(checkoutDialog.this, 
					"Are you sure to check out?", "Checkout", JOptionPane.YES_NO_OPTION);
			if (ans == JOptionPane.YES_OPTION){
				int roomNum = (int) roomComboBox.getSelectedItem();
				//um.updateUser(um.getCurrentUser());
				if (roomNum != -1){
					rm.searchRoom(roomNum).initialize();
					rm.deleteHost(h);
					System.out.print("Check out: \t"+"Room "+roomNum+"\t Host: "+h.getName());
					checkoutDialog.this.setVisible(false);
				}
				else JOptionPane.showMessageDialog(checkoutDialog.this, "Error: The Room Number cannot be -1", "Error", JOptionPane.ERROR_MESSAGE);
			}
		}
		
	}
	
	class CancelHandler implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			checkoutDialog.this.setVisible(false);
		}
		
	}
	
}
